<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld"%>
<%@ taglib prefix="my" uri="/WEB-INF/tld/my.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${projectName}-编辑</title>
<jsp:include page="/WEB-INF/view/inc/css.jsp"></jsp:include>
</head>
<body class="cld-body">
	<div class="enter-panel ep-sm">
		<input type="hidden" id=aatdId value="${param.aatdId}"/>
		<input type="hidden" id="aatId" value="${param.aatId}"/>
  		<div class="form-group">
			<label for="orderby" class="col-sm-4">执行序号 <span class="text-danger">*</span></label>
			<div class="col-sm-8"><input type="text" class="form-control" id="orderby" placeholder="执行序号" value="${appApiTestDtl.orderby}"></div>
		</div>
  		<div class="form-group">
			<label for="code" class="col-sm-4">服务 <span class="text-danger">*</span></label>
			<div class="col-sm-8"><input type="text" class="form-control" id="prjName" placeholder="请选择服务" value="${appApiTestDtl.prjName}" onblur="info.changePrj()" autocomplete="off" />
				<input type="hidden" id="prjId" value="${appApiTestDtl.prjId}"/>
			</div>
		</div>
  		<div class="form-group">
			<label for="path" class="col-sm-4">接口 <span class="text-danger">*</span></label>
			<div class="col-sm-8"><input type="text" class="form-control" id="pathName" placeholder="接口地址" value="${appApiTestDtl.path}" onblur="info.changePath()" autocomplete="off" />
				<input type="hidden" id="path" value="${appApiTestDtl.path}"/>
			</div>
		</div>
  		<div class="form-group">
			<label for="remark" class="col-sm-4">入参信息</label>
			<div class="col-sm-8"></div>
			<table class="table table-striped table-hover">
				<thead>
				<tr class="info">
                      <th width="130">参数</th>
                      <th width="280">说明</th>
                      <th width="250">类型</th>
				</tr>
                   </thead>
                   <tbody id="paramsTbody"></tbody>
			</table>
		</div>
		<div class="form-group">
			<label for="succResult" class="col-sm-4 va-top">返回结果</label>
			<div class="col-sm-8"><textarea class="form-control" id="succResult" rows="3" placeholder="返回结果 variableValue代办变动值">${appApiTestDtl.succResult}</textarea></div>
		</div>
  		<div class="footer-operate">
			<span id="saveMsg" class="label label-danger"></span>
 			<div class="btn-group">
				<button type="button" id="saveBtn" class="btn btn-success enter-fn">保存</button>
			</div>
		</div>
	</div>

	<jsp:include page="/WEB-INF/view/inc/js.jsp"></jsp:include>
	<script type="text/javascript">
	var info = {
			changePrj: function() {
				var prjId = $('#prjId').val();
				if (JUtil.isEmpty(prjId)) {
					return;
				}
				$('#pathName').typeahead('destroy');
				JUtil.sys.typeahead({
					id: 'pathName',
					valueId: 'path',
					url: webroot + '/prjApi/f-json/findSearch?type=kvEntity&prjId=' + prjId
				});
			},
			changePath: function() {
				var prjId = $('#prjId').val();
				var path = $('#path').val();
				if (JUtil.isEmpty(path)) {
					return;
				}
				JUtil.ajax({
					url: '${webroot}/prjApi/f-json/get',
					data: {
						prjId: prjId,
						path: path
					},
					success : function(json) {
						if (json.code === 0) {
							var paramsTbody = $('#paramsTbody').empty();
							var content = [];
							$.each($.parseJSON(json.body.params), function(i, item) {
								content.push('<tr>',
										'<td><input type="text" id="param_',item.code,'" name="',item.code,'" style="width: 250px;" value="',item.value,'" placeholder="入参的值" title="no1_code:取执行序号1返回为code的值，目前支持到两级如:no1_body.id"/></td>',
										'<td>',item.code,':',item.name,'</td>',
										'<td>',item.clazz,'</td>',
										'</tr>');
							});
							paramsTbody.append(content.join(''));
							var params = '${appApiTestDtl.params}';
							if (params != '') {
								var paramJson = $.parseJSON(params);
								$.each(paramJson, function(i, item) {
									$('#param_' + item.key).val(item.value);
								});
							}
						}
						else if (json.code === -1)
							message(JUtil.msg.ajaxErr);
						else
							message(json.message);
					}
				});
			}
	};
	$(function() {

		JUtil.sys.typeahead({
			id: 'prjName',
			valueId: 'prjId',
			url: webroot + '/appInfo/f-json/findSearchPrj?aiId=${param.aiId}'
		});
		if ($('#aatdId').val() == '') {
			$('#succResult').val('{code:0}');
		} else {
			info.changePath();
		}
		
		$('#saveBtn').click(function() {
			var saveMsg = $('#saveMsg').empty();
			var aiId = $('#aiId');
			
			var orderby = $('#orderby');
			if(JUtil.isEmpty(orderby.val())) {
				saveMsg.append('请输入执行序号');
				orderby.focus();
				return;
			}
			var prjId = $('#prjId');
			if(JUtil.isEmpty(prjId.val())) {
				saveMsg.append('请选择服务');
				prjId.focus();
				return;
			}
			var path = $('#path');
			if(JUtil.isEmpty(path.val())) {
				saveMsg.append('请选择接口地址');
				path.focus();
				return;
			}
			var params = [];
			$('#paramsTbody').find('input').each(function(i, item) {
				var obj = $(item);
				var id = obj.attr('id');
				if (id.indexOf('param_')!=-1) {
					params.push({key:obj.attr('name'),value:obj.val()});
				}
			});
			
			JUtil.ajax({
				eventId: 'saveBtn',
				url: '${webroot}/appApiTest/f-json/saveDtl',
				data: {
					aatdId: $('#aatdId').val(),
					aatId: $('#aatId').val(),
					orderby: orderby.val(),
					prjId: prjId.val(),
					path: path.val(),
					params: JSON.stringify(params),
					succResult: $('#succResult').val()
				},
				success : function(json) {
					if (json.code === 0) {
						saveMsg.attr('class', 'label label-success').append('保存成功');
						setTimeout(function() {
							parent.apiAutoTestDtlInfo.loadInfo();
							parent.dialog.close();
						}, 800);
					}
					else if (json.code === -1)
						saveMsg.append(JUtil.msg.ajaxErr);
					else
						saveMsg.append(json.message);
				}
			});
		});
	});
	</script>
</body>
</html>